package thread;

import java.util.concurrent.*;

/**
 * @description: 创建现成的方式4，通过线程池
 * @author: apple
 * @date:  2021/12/5 10:11 下午
 * @version 1.0
 **/
public class ThreadPoolTest {

    public static void main(String[] args) {

        ExecutorService executorService = Executors.newFixedThreadPool(10);


        // 线程池的管理
        System.out.println(executorService.getClass());

        executorService.execute(new NN()); // 适用于Runnable
        executorService.execute(new NNN()); // 适用于Runnable
        Future<Integer> result = executorService.submit(new NNNN());// 适用于Callable\
        try {
            Integer i = result.get();
            System.out.println("结果是：" + i);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

        executorService.shutdown();

    }
}
class NN implements Runnable {
    @Override
    public void run() {

        System.out.println("偶数");
        for (int i = 0; i < 10; i++) {
            if (i % 2 == 0) {
                System.out.println(Thread.currentThread().getName() + "," + i);
            }
        }
    }
}

class NNN implements Runnable {
    @Override
    public void run() {
        System.out.println("奇数");
        for (int i = 0; i < 10; i++) {
            if (i % 2 != 0) {
                System.out.println(Thread.currentThread().getName() + "," + i);
            }
        }
    }
}


class NNNN implements Callable<Integer> {
    @Override
    public Integer call() throws Exception {
        System.out.println("求合");
        int sum = 0;
        for (int i = 0; i < 100; i++) {
            sum = sum + i;
        }
        return sum;
    }
}


